<?php 

class Category_model extends CI_Model{
	public function __construct() {
        parent::__construct();

    }
	
	function create($data){
		$data = array(
					'category_name' => $this->input->post('category_name'),
					'top' =>  $this->input->post('top'),
					'parent' => $this->input->post('parent'),
					'status' => $this->input->post('status'),
					'sort_order' => $this->input->post('sort_order'),
					'date_add' => date ('Y-m-d H:i:s')
				);
		$this->db_front->insert('product_category',$data);
	}
	
	function getAll($page='', $limit=''){
		if(!empty($limit))
		$this->db_front->limit($limit,$page);
		
		$query = $this->db_front->get('product_category');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	/*function getAll($page='', $limit='',$where=''){
		if(!empty($limit))
		$this->db_front->limit($limit,$page);
		
		$query = $this->db_front->query('select * from product_category');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}*/
	
	function update($category_id){
		$data = array(
					'category_name' => $this->input->post('category_name'),
					'top' =>  $this->input->post('top'),
					'parent' => $this->input->post('parent'),
					'status' => $this->input->post('status'),
					'sort_order' => $this->input->post('sort_order'),
					'date_modified' => date ('Y-m-d H:i:s')
				);
        $this->db_front->where('category_id', $category_id);
        $this->db_front->update('product_category', $data);
    }
	
	function select($category_id){
        return $this->db_front->get_where('product_category', array('category_id'=>$category_id))->row();
    }
	
	function delete($category_id){
        $this->db_front->delete('product_category', array('category_id'=>$category_id));
    }
	
	function get_parent(){
		$this->db_front->where('parent', 0);
		$this->db_front->order_by('category_name ASC');
		$query = $this->db_front->get('product_category');
		if ($query->num_rows() > 0) return $query->result();
		return NULL;
	}
}
?>